<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 3px 3px 3px 3px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户账号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-refresh-btn"><i class="layui-icon layui-icon-refresh"></i> 刷新
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="power">授权</button>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="updatePwd">修改密码</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="/static/js/common.js"></script>
<script>
    var lay, form, table;
    var user = $.common.getUserInfo();
    var roleType = user.roleType;
    let roleTypeArray = [
        {"key": "超级管理员", "value": 0},
        {"key": "管理员", "value": 1},
        {"key": "普通账号", "value": 2}];
    let powerDataTypeArray = [
        {"key": "修改", "value": 0},
        {"key": "查看", "value": 1},
        {"key": "无权限", "value": 2}];
    layui.use(['form', 'table'], function () {
        lay = layui.jquery;
        form = layui.form;
        table = layui.table;
        table.render({
            elem: '#currentTableId',
            url: '/users/pages',
            toolbar: '#toolbarDemo',
            method: "post",
            request: {
                pageName: "pageNum",
                limitName: "pageSize"
            },
            contentType: "application/json; charset=utf-8",
            where: {
                param: {}
            },
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 65, title: 'ID', sort: true},
                {field: 'userName', width: 200, title: '账号'},
                {field: 'name', width: 200, title: '姓名'},
                {
                    field: 'roleType', width: 200, title: '角色类型', templet: function (a) {
                        for (let type in roleTypeArray) {
                            if (roleTypeArray[type].value == a.roleType) {
                                return roleTypeArray[type].key;
                            }
                        }
                        return '';
                    }
                },
                {
                    field: 'powerDataType', width: 200, title: '数据权限类型',
                    templet: function (a) {
                        for (let type in powerDataTypeArray) {
                            if (powerDataTypeArray[type].value == a.powerDataType) {
                                return powerDataTypeArray[type].key;
                            }
                        }
                        return '';
                    }
                },
                {field: 'powerLastTime', width: 200, title: '数据权限过期时间'},
                // {field: 'longTime', width: 200, title: '登录时间' },
                {title: '操作', minWidth: 120, toolbar: '#currentTableBar', align: "center", ixed: 'right'}
            ]],
            limits: [5, 10, 15, 20, 25, 50, 100, 500],
            limit: 10,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            search(data);
            return false;
        });

        form.on('submit(data-refresh-btn)', function (data) {
            // 提交刷新了当前页面
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                if (0 != roleType) {
                    $.common.showErrorMessage("没有操作权限");
                    return false;
                }
                var index = layer.open({
                    title: '添加账号',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['80%', '80%'],
                    content: '/static/page/users/add.html',
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'power') {
                if (1 < roleType) {
                    $.common.showErrorMessage("没有操作权限");
                    return false;
                }
                let checkStatus = table.checkStatus('currentTableId');
                if (checkStatus.data.length != 1) {
                    $.common.showErrorMessage("请选择一条数据");
                    return false;
                }
                let data = checkStatus.data[0];
                var index = layer.open({
                    title: '授权',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '50%'],
                    content: '/static/page/users/power.html?powerDataType='+data.powerDataType,
                    data: data,
                    success: function (index, layero) {
                        var body = layer.getChildFrame('body', layero);
                        for (let key in data) {
                            body.find("#" + key).val(data[key]);
                        }
                    },
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {

        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                if (0 != roleType) {
                    $.common.showErrorMessage("没有操作权限");
                    return false;
                }
                var index = layer.open({
                    title: '修改账号',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['80%', '80%'],
                    content: '/static/page/users/add.html?roleType=' + data.roleType + "&powerDataType=" + data.powerDataType,
                    data: data,
                    success: function (index, layero) {
                        var body = layer.getChildFrame('body', layero);
                        for (let key in data) {
                            body.find("#" + key).val(data[key]);
                        }
                    },
                    end: function () {

                    }
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'updatePwd') {
                if (0 != roleType) {
                    $.common.showErrorMessage("没有操作权限");
                    return false;
                }
                var index = layer.open({
                    title: '修改密码',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['80%', '80%'],
                    content: '/static/page/users/updatePwd.html?id='+data.id,
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                if (0 != roleType) {
                    $.common.showErrorMessage("没有操作权限");
                    return false;
                }
                layer.confirm('确定删除？', function (index) {
                    let url = "/users/delete/" + obj.data.id;
                    let callback = {
                        onSuccess: function (result) {
                            if (result.code == 0) {
                                window.location.reload();
                            } else {
                                $.common.showErrorMessage(result.message);
                                return false;
                            }
                        }
                    };
                    $.xAjax(url, 'POST', JSON.stringify(data.field), callback);
                });
            }
        });

        function search(data) {
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    param: data.field
                }
            }, 'data');
        }
    })
</script>

</body>
</html>
